package com.ruoyi.assessment.controller;

import com.github.pagehelper.Page;
import com.ruoyi.assessment.entity.AssessmentVo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.incident.domain.XlkjIncident;
import com.ruoyi.incident.service.IXlkjIncidentService;
import com.ruoyi.ini.domain.XlkjIni;
import com.ruoyi.ini.service.IXlkjIniService;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 部门协同绩效考核
 */
@RestController
@RequestMapping("/assessment/xie")
public class AssessmentControllerXie extends BaseController {

    @Autowired
    private ISysUserService sysUserService;
//    @Autowired
//    private IXlkjIncidentService incidentService;
    @Autowired
    private IXlkjIniService iniService;
    @Autowired
    private SysDeptMapper sysDeptMapper;

    @GetMapping("/list")
    public TableDataInfo list(){
//        startPage();
//        List<AssessmentVo> assessmentVoList = new ArrayList<>();

        //查询所有协同部门
        startPage();
        Page<SysUser> sysUsers = (Page<SysUser>) sysUserService.selectUserListByPostId(3L);
        Page<AssessmentVo> assessmentVoList=new Page<>();
        assessmentVoList.setTotal(sysUsers.getTotal());
        for(SysUser sysUser:sysUsers){
            AssessmentVo assessmentVo = new AssessmentVo();
            assessmentVo.setSysUser(sysUser);

//            //查询事件总数（需协助事件）以及完成总数（反馈意见）
//            XlkjIncident incident = new XlkjIncident();
//            incident.setAssist("Y");
//            List<XlkjIncident> xlkjIncidents = incidentService.selectXlkjIncidentList(incident);
//            assessmentVo.setIncidentNum(xlkjIncidents.size());
//            incident.setOpinionNull("abc");
//            List<XlkjIncident> xlkjIncidents1 = incidentService.selectXlkjIncidentList(incident);
//            assessmentVo.setIncidentSucc(xlkjIncidents1.size());

            XlkjIni ini = new XlkjIni();
            ini.setAssist("Y");
            List<XlkjIni> xlkjInis = iniService.selectXlkjIniList(ini);
            assessmentVo.setIncidentNum(xlkjInis.size());
            ini.setOpinionNull("abc");
            List<XlkjIni> xlkjInis2 = iniService.selectXlkjIniList(ini);
            assessmentVo.setIncidentSucc(xlkjInis2.size());

            //设置总分值
            assessmentVo.setSource(100);

            /**
             * 计算得分
             *    10% <= 完成总数 / 事件总数  < 20%     分值 + 10
             *    20% <= 完成总数  / 事件总数  < 50%    分值 + 30
             *    50% <= 完成总数 / 事件总数   < 70%     分值 + 50
             *    70% <= 完成总数 / 事件总数   <80%     分值 + 70
             *    80% <= 完成总数 / 事件总数   <90      分值 + 85
             *    90% <= 完成总数 / 事件总数            分值 + 100
             */

            /**
             * 计算得分:
             *      该部门完成协助数 / 需要该部门协助事件总数 * 100
             */
            Integer source = 0;

            Integer fl = assessmentVo.getIncidentSucc()/assessmentVo.getIncidentNum();

            source += fl * 100;
//            if(fl >= 0.9){
//                source += 100;
//            }else if(fl >= 0.8){
//                source += 85;
//            }else if(fl >= 0.7){
//                source += 70;
//            }else if(fl >= 0.5){
//                source += 50;
//            }else if(fl >= 0.2){
//                source += 30;
//            }else if(fl >= 0.1){
//                source += 10;
//            }

            assessmentVo.setSourceNum(source);

            assessmentVoList.add(assessmentVo);
        }
        return getDataTable(assessmentVoList);
    }

}
